package com.wonder.common.common;

import org.aspectj.bridge.MessageUtil;

import com.wonder.common.constants.Constants;
import com.wonder.common.exception.ExceptionCodeEnum;
import com.wonder.common.exception.ExceptionHandler;
import com.wonder.common.utils.MessageUtils;

public class AjaxResult<T> {
    private long status;
    private String msg;
    private T data;
    private String msgId;

    protected AjaxResult() {
    }

    protected AjaxResult(long code, String message, T data) {
        this.status = code;
        this.msg = message;
        this.data = data;
    }
    
    protected AjaxResult(long code, String message, T data, String msgId) {
        this.status = code;
        this.msg = message;
        this.data = data;
        this.msgId=msgId;
        
    }

    /**
     * 成功返回结果
     */
    public static <T> AjaxResult<T> success() {
        return new AjaxResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getMessage(), null,ExceptionCodeEnum.SUCCESS.getMsgId());
    }

    /**
     * 成功返回结果
     */
    public static <T> AjaxResult<T> success(String message) {
        return new AjaxResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), message, null,message);
    }
    
    /**
     * success message with system message id
     */
    public static <T> AjaxResult<T> success(String message, String msgId) {
        return new AjaxResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), message, null,msgId);
    }
    
    /**
     * 成功返回结果
     */
    public static <T> AjaxResult<T> formatSuccess(String message) {
        return new AjaxResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), MessageUtils.message(message), null, ExceptionCodeEnum.SUCCESS.getMsgId());
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> AjaxResult<T> success(T data) {
        return new AjaxResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getMessage(), data, ExceptionCodeEnum.SUCCESS.getMsgId());
    }
    
    /**
     * 成功返回结果数据为字符串
     *
     * @param data 获取的数据
     */
    public static <T> AjaxResult<T> data(T data) {
        return new AjaxResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), ExceptionCodeEnum.SUCCESS.getMessage(), data, ExceptionCodeEnum.SUCCESS.getMsgId());
    }


    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     * @param  message 提示信息
     */
    public static <T> AjaxResult<T> success(T data, String message) {
        return new AjaxResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), message, data, ExceptionCodeEnum.SUCCESS.getMsgId());
    }
    
    /**
     * return success message data, message and message id
     * 
     * @param <T>
     * @param data
     * @param message
     * @param msgId
     * @return
     */
    public static <T> AjaxResult<T> success(T data, String message, String msgId) {
        return new AjaxResult<T>(ExceptionCodeEnum.SUCCESS.getCode(), message, data,msgId);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     */
    public static <T> AjaxResult<T> failed(ExceptionHandler errorCode) {
        System.out.println("errorCode1:" + errorCode);
        return new AjaxResult<T>(errorCode.getCode(), errorCode.getMessage(), null, ExceptionCodeEnum.FAILED.getMsgId());
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     * @param message 错误信息
     */
    public static <T> AjaxResult<T> failed(ExceptionHandler errorCode,String message) {
        System.out.println("errorCode2:" + errorCode);
        return new AjaxResult<T>(errorCode.getCode(), message, null, message);
    }
    

   /**
    * return fail with errorCode, message and msgId
    * @param <T>
    * @param errorCode
    * @param message
    * @param msgId
    * @return
    */
   public static <T> AjaxResult<T> failed(ExceptionHandler errorCode,String message, String msgId) {
       System.out.println("errorCode2:" + errorCode);
       return new AjaxResult<T>(errorCode.getCode(), message, null, msgId);
   }
    
    /**
     * 失败返回结果
     * @param errorCode 错误码
     * @param message 错误信息
     */
    public static <T> AjaxResult<T> failedWithData(T data,String message) {  
        return new AjaxResult<T>(ExceptionCodeEnum.FAILED.getCode(), message, data, ExceptionCodeEnum.FAILED.getMsgId());
    }


    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> AjaxResult<T> failed(String message) {
        return new AjaxResult<T>(ExceptionCodeEnum.FAILED.getCode(), message, null, message);
    }
    
    /**
     * return fail with fail message and message id
     * @param message 提示信息
     * @param msgId error msg id
     */
    public static <T> AjaxResult<T> failed(String message, String msgId) {
        return new AjaxResult<T>(ExceptionCodeEnum.FAILED.getCode(), message, null, msgId);
    }
    
    /**
    * 失败返回结果
    * @param message 提示信息
    */
   public static <T> AjaxResult<T> formatFailed(String message) {
       return new AjaxResult<T>(ExceptionCodeEnum.FAILED.getCode(), MessageUtils.message(message), null, ExceptionCodeEnum.FAILED.getMsgId());
   }

    /**
     * 失败返回结果
     */
    public static <T> AjaxResult<T> failed() {
        return failed(ExceptionCodeEnum.FAILED, ExceptionCodeEnum.FAILED.getMsgId());
    }
    
    
 
    /**
     * 失败返回结果
     */
    public static <T> AjaxResult<T> toAjax(boolean ret) {
    	if(ret)
    		return success(Constants.DEFAULT_AJAX_SUCCESS);
        return failed(Constants.DEFAULT_AJAX_FAILOR);
    }
    
    
    /**
     * 失败返回结果
     */
    public static <T> AjaxResult<T> toAjax(int ret) {
    	if(ret>0)
    		return success(Constants.DEFAULT_AJAX_SUCCESS);
        return failed(Constants.DEFAULT_AJAX_FAILOR);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> AjaxResult<T> validateFailed() {
        return failed(ExceptionCodeEnum.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static <T> AjaxResult<T> validateFailed(String message) {
        return new AjaxResult<T>(ExceptionCodeEnum.VALIDATE_FAILED.getCode(), message, null, ExceptionCodeEnum.VALIDATE_FAILED.getMsgId());
    }
    
    /**
     * return validate fail error with message and msg id
     * @param <T>
     * @param message
     * @param msgId
     * @return
     */
    public static <T> AjaxResult<T> validateFailed(String message, String msgId) {
        return new AjaxResult<T>(ExceptionCodeEnum.VALIDATE_FAILED.getCode(), message, null, msgId);
    }

    /**
     * 未登录返回结果
     */
    public static <T> AjaxResult<T> unauthorized(T data) {
        return new AjaxResult<T>(ExceptionCodeEnum.UNAUTHORIZED.getCode(), ExceptionCodeEnum.UNAUTHORIZED.getMessage(), data, ExceptionCodeEnum.UNAUTHORIZED.getMsgId());
    }

    /**
     * 未登录返回结果
     */
    public static <T> AjaxResult<T> unauthorized() {
        return new AjaxResult<T>(ExceptionCodeEnum.UNAUTHORIZED.getCode(), ExceptionCodeEnum.UNAUTHORIZED.getMessage(), null,ExceptionCodeEnum.UNAUTHORIZED.getMsgId());
    }

    /**
     * 没有权限查看
     */
    public static <T> AjaxResult<T> forbidden() {
        return new AjaxResult<T>(ExceptionCodeEnum.FORBIDDEN.getCode(), ExceptionCodeEnum.FORBIDDEN.getMessage(), null, ExceptionCodeEnum.FORBIDDEN.getMsgId());
    }

    /**
     * 未授权返回结果
     */
    public static <T> AjaxResult<T> forbidden(T data) {
        return new AjaxResult<T>(ExceptionCodeEnum.FORBIDDEN.getCode(), ExceptionCodeEnum.FORBIDDEN.getMessage(), data,ExceptionCodeEnum.FORBIDDEN.getMsgId());
    }

    public long getStatus() {
        return status;
    }

    public void setStatus(long code) {
        this.status = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String message) {
        this.msg = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

	public String getMsgId() {
		return msgId;
	}

	public void setMsgId(String errorId) {
		this.msgId = errorId;
	}

	
}
